package com.ysh.springmvc.interceptors;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 袁胜虎 on 2022/8/30 22:21
 */
@Component
public class LoginInteceptor implements HandlerInterceptor {
    /**
     * 在到达目标方法之前执行
     *
     * @param request
     * @param response
     * @param handler
     * @return 返回true代表放行, false代表拦截
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.err.println("LoginInteceptor中的方法preHandle执行了!!!");
        Object user = request.getSession().getAttribute("user");
        if (user == null){
            response.getWriter().write("denglu!!");
        }
        return user != null;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.err.println("LoginInteceptor中的方法postHandle执行了!!!");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.err.println("LoginInteceptor中的方法afterCompletion执行了!!!");
    }
    
}
